package com.school.service.impl;

import com.school.entity.AuditInfoRecord;
import com.school.repository.AuditInfoRecordRepository;
import com.school.service.AuditInfoRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AuditInfoRecordServiceImp implements AuditInfoRecordService {
    @Autowired
    AuditInfoRecordRepository auditInfoRecordRepository;

    /**
     * 增加
     * @param auditInfoRecord
     * @return
     */
    @Override
    public AuditInfoRecord addAuditIndoRecord(AuditInfoRecord auditInfoRecord) {
        auditInfoRecord.setCreateDate(new Date());
        auditInfoRecord.setUpdateDate(new Date());
        return auditInfoRecordRepository.save(auditInfoRecord);
    }

    /**
     * 修改
     * @param auditInfoRecord
     * @return
     */
    @Override
    public AuditInfoRecord updateAuditIndoRecord(AuditInfoRecord auditInfoRecord) {
        auditInfoRecord.setUpdateDate(new Date());
        return auditInfoRecordRepository.saveAndFlush(auditInfoRecord);
    }

    /**
     * 查看详情
     * @param complaintInfoId
     * @return
     */
    @Override
    public List<AuditInfoRecord> getListByComplaintInfoId(String complaintInfoId) {
        return auditInfoRecordRepository.findAuditInfoRecordsByComplaintInfoId(complaintInfoId);
    }

    /**
     * 根据申诉记录ID，维度，分类查询申诉记录
     * @param auditInfoRecord
     * @return
     */
    @Override
    public List<AuditInfoRecord> getListByRecordInfo(AuditInfoRecord auditInfoRecord) {
        Specification querySpecification = new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(auditInfoRecord != null)
                {
                    String complaintInfoId = auditInfoRecord.getComplaintInfoId();
                    Integer dimension = auditInfoRecord.getDimension();
                    Integer classify = auditInfoRecord.getClassify();
                    if (!StringUtils.isEmpty(complaintInfoId))
                    {
                        predicates.add(criteriaBuilder.equal(root.get("complaintInfoId").as(String.class), complaintInfoId));
                    }
                    if (!StringUtils.isEmpty(dimension))
                    {
                        predicates.add(criteriaBuilder.equal(root.get("dimension").as(Integer.class), dimension));
                    }
                    if (!StringUtils.isEmpty(classify))
                    {
                        predicates.add(criteriaBuilder.equal(root.get("classify").as(Integer.class), classify));
                    }
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return auditInfoRecordRepository.findAll(querySpecification);
    }

}
